package com.customview.wzq.recycleviewdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * 创建时间：2017/7/12
 * 作者：kb_jay
 * 功能描述：
 */

public class RecyclerDivider extends RecyclerView.ItemDecoration {


    private Drawable mDivider;

    public RecyclerDivider(Context context) {
        mDivider = context.getResources().getDrawable(R.drawable.divider);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //设置偏移量
        outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        //设置mDivider的位置
        for (int i = 0; i < parent.getChildCount(); i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
            int left = child.getLeft()-lp.leftMargin;
            int right = child.getRight()+lp.rightMargin;
            int top = child.getBottom()+lp.bottomMargin;
            int bottom = top +mDivider.getIntrinsicHeight();
            mDivider.setBounds(left,top,right,bottom);
            mDivider.draw(c);
        }
    }
}
